const createLinkedList = require('../utils/createLinkList')
const { reverseList } = require('./反转链表')


var isPalindrome = function (head) {
    if (head === null || head.next === null) return true;
    let fast = head, slow = head
    // 1. 快慢指针找中点
    while (fast !== null && fast.next !== null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    let reversedSecondHalf = reverseList(slow);

    let p1 = head, p2 = reversedSecondHalf;
    while (p2 !== null) {
        if (p1.val !== p2.val) {
            return false;
        }
        p1 = p1.next;
        p2 = p2.next;
    }

    return true;
}


const head = createLinkedList([1, 2, 2, 1])
const result = isPalindrome(head)
console.log("🚀 ~ result:", result)
